package com.example.memorypoint.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.memorypoint.DateBase.DiaryDatabase;
import com.example.memorypoint.R;
import com.example.memorypoint.mvp.date.DateContract;
import com.example.memorypoint.mvp.date.DatePresenterImpl;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarLayout;
import com.haibin.calendarview.CalendarView;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class DateActivity extends AppCompatActivity implements DateContract.DataView, CalendarView.OnYearChangeListener, CalendarView.OnCalendarSelectListener {

    private CalendarView mCalendarView;
    private TextView mTextMonthDay;
    private TextView mTextYear;
    private TextView mTextLunar;
    private TextView mTextCurrentDay;
    private TextView tv_first_data;
    private TextView tv_second_data;
    private CalendarLayout mCalendarLayout;
    private RelativeLayout mRelativeTool;
    private int mYear;
    private Toolbar toolbar;

    private DatePresenterImpl presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date);

        initView();
        mYear = mCalendarView.getCurYear();
        toolbarBack();

        // 创建 Presenter 实例
        presenter = new DatePresenterImpl(this, new DiaryDatabase(getBaseContext()));
        presenter.loadData(getToday());
    }

    private void initView() {
        mTextMonthDay = findViewById(R.id.tv_month_day);
        mTextYear = findViewById(R.id.tv_year);
        mTextLunar = findViewById(R.id.tv_lunar);
        mRelativeTool = findViewById(R.id.rl_tool);
        mCalendarView = findViewById(R.id.calendarView);
        mTextCurrentDay = findViewById(R.id.tv_current_day);
        mCalendarLayout = findViewById(R.id.calendarLayout);
        tv_first_data = findViewById(R.id.tv_first_data);
        tv_second_data = findViewById(R.id.tv_second_data);
        toolbar = findViewById(R.id.toolbar);

        mCalendarView.setOnYearChangeListener(this);
        mCalendarView.setOnCalendarSelectListener(this);

        mTextMonthDay.setOnClickListener(v -> mTextMonthDay.setText(String.valueOf(mYear)));
        findViewById(R.id.fl_current).setOnClickListener(v -> mCalendarView.scrollToCurrent());

        initDate();
    }

    private void initDate() {
        mTextYear.setText(String.valueOf(mCalendarView.getCurYear()));
        mTextMonthDay.setText(mCalendarView.getCurMonth() + "月" + mCalendarView.getCurDay() + "日");
        mTextLunar.setText("今日");
        mTextCurrentDay.setText(String.valueOf(mCalendarView.getCurDay()));
    }

    private void toolbarBack() {
        toolbar.setNavigationOnClickListener(view -> finish());
    }

    private String getToday() {
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return today.format(formatter);
    }

    private String formatDate(int year, int month, int day) {
        return String.format("%04d-%02d-%02d", year, month, day);
    }

    @Override
    public void onYearChange(int year) {
        mTextMonthDay.setText(String.valueOf(year));
    }

    @Override
    public void onCalendarOutOfRange(Calendar calendar) {
    }

    @Override
    public void onCalendarSelect(Calendar calendar, boolean isClick) {
        mTextLunar.setVisibility(View.VISIBLE);
        mTextYear.setVisibility(View.VISIBLE);
        mTextMonthDay.setText(calendar.getMonth() + "月" + calendar.getDay() + "日");
        mTextYear.setText(String.valueOf(calendar.getYear()));
        mTextLunar.setText(calendar.getLunar());
        mYear = calendar.getYear();

        String selectedDate = formatDate(calendar.getYear(), calendar.getMonth(), calendar.getDay());
        presenter.loadData(selectedDate);
    }

    @Override
    public void showData(String diaryCount, String wordCount) {
        tv_first_data.setText(diaryCount);
        tv_second_data.setText(wordCount);
    }

    @Override
    public void showError(String error) {
        Log.e("DateActivity", error);
    }
}
